package cn.itsource.blog.controller;

import cn.itsource.blog.anno.PreAuthorize;
import cn.itsource.blog.domain.Article;
import cn.itsource.blog.query.ArticleQuery;
import cn.itsource.blog.service.IArticleService;
import cn.itsource.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private IArticleService articleService;

    @PatchMapping
    public JsonResult queryPage(@RequestBody ArticleQuery articleQuery) {
        try{
            return JsonResult.success(articleService.pageInfo(articleQuery));
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }

    @PutMapping
    @PreAuthorize(name = "文章新增",sn = "article:save",descs = "就是测试一把")
    public JsonResult save(@RequestBody Article article) {
        try{
            articleService.save(article);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }



    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id) {
        try{
            articleService.delete(id);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }

    @PatchMapping("/batchDelete")
    public JsonResult batchDelete(@RequestBody List<Article> users){
        try{
            articleService.batchDelete(users);
            return JsonResult.success();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }

  


}
